package com.tjp.eight.ui.fragment.mydynamic;

import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tjp.eight.R;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.views.lazyviewpager.view.indicator.IndicatorViewPager;
import com.tjp.eight.views.lazyviewpager.view.indicator.ScrollIndicatorView;
import com.tjp.eight.views.lazyviewpager.view.indicator.slidebar.ColorBar;
import com.tjp.eight.views.lazyviewpager.view.indicator.transition.OnTransitionTextListener;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/2/28.
 */
public class FragmentMyDynamic extends BaseFragment<MyDynamicPresenter> {
    private IndicatorViewPager indicatorViewPager;
    private ScrollIndicatorView sv_my_dynamic;
    private ViewPager vp_my_dynamic;
    private ArrayList<String> tabList;

    @Override
    protected MyDynamicPresenter createPresenter() {
        return null;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_my_dynamic, container, false);
        }
        initViews();
        return contentView;
    }

    void initViews() {
        tabList = new ArrayList<>();
        tabList.add("我的评论");
        tabList.add("我的分享");
        sv_my_dynamic = (ScrollIndicatorView) contentView.findViewById(R.id.sv_my_dynamic);
        ColorBar colorBar = new ColorBar(getActivity(), 0xffffcc33, 5);
        Point point = new Point();
        getActivity().getWindowManager().getDefaultDisplay().getSize(point);
        int screenW = point.x;
        int barW = (int) (screenW / 2 * 0.5);
        colorBar.setWidth(barW);
        sv_my_dynamic.setScrollBar(colorBar);
        vp_my_dynamic = (ViewPager) contentView.findViewById(R.id.vp_my_dynamic);
        vp_my_dynamic.setOffscreenPageLimit(1);
        // 设置滚动监听
        int selectColorId = R.color.title_color;
        int unSelectColorId = R.color.item_is_read;
        sv_my_dynamic.setOnTransitionListener(new OnTransitionTextListener().setColorId(getActivity(), selectColorId, unSelectColorId));
        indicatorViewPager = new IndicatorViewPager(sv_my_dynamic, vp_my_dynamic);

        indicatorViewPager.setAdapter(new MyDynamicAdapter(getChildFragmentManager()));
    }

    class MyDynamicAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter {
        public MyDynamicAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public int getCount() {
            return tabList == null ? 1 : tabList.size();
        }

        @Override
        public View getViewForTab(int position, View convertView, ViewGroup container) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.home_types, container, false);
            }
            TextView textView = (TextView) convertView;
            if (tabList != null)
                textView.setText(tabList.get(position % tabList.size()));
            return convertView;
        }

        @Override
        public Fragment getFragmentForPage(int position) {
            FragmentDynamicType dynamicType = new FragmentDynamicType();

            return dynamicType;
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }
    }
}
